package ar.com.uade.administradorsiembra.vista;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import ar.com.uade.administradorsiembra.controladores.ABMCerealController;
import ar.com.uade.administradorsiembra.modelo.exceptions.CerealInexistente;
import ar.com.uade.administradorsiembra.views.*;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class EliminarCerealGUI extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel jCerealLabel;
	private JComboBox jCerealComboBox;
	private JButton jCancelarButton;
	private JButton jEliminarButton;

	private ABMCerealController cerealController;
	private List<CerealView> cerealesView;
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void iniciar() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				EliminarCerealGUI inst = new EliminarCerealGUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public EliminarCerealGUI() {
		super();
		initGUI();
		this.cerealController = ABMCerealController.getInstancia();
		cargarCereales();
	}
	
	private void cargarCereales(){
		cerealesView = cerealController.getCereales();
		
		for (CerealView cerealView : cerealesView) {
			jCerealComboBox.addItem(cerealView.getDescripcion());
		}
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jCerealLabel = new JLabel();
				getContentPane().add(jCerealLabel);
				jCerealLabel.setText("Cereal");
				jCerealLabel.setBounds(12, 31, 62, 16);
			}
			{
				ComboBoxModel jCerealComboBoxModel = 
					new DefaultComboBoxModel(
							new String[] {  });
				jCerealComboBox = new JComboBox();
				getContentPane().add(jCerealComboBox);
				jCerealComboBox.setModel(jCerealComboBoxModel);
				jCerealComboBox.setBounds(74, 28, 88, 23);
			}
			{
				jEliminarButton = new JButton();
				getContentPane().add(jEliminarButton);
				jEliminarButton.setText("Eliminar");
				jEliminarButton.setBounds(180, 71, 76, 23);
				jEliminarButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jEliminarButtonActionPerformed(evt);
						JOptionPane.showMessageDialog(new JLabel("todo ok"), "Se eliminaron los datos en forma exitosa");
					}
				});
			}
			{
				jCancelarButton = new JButton();
				getContentPane().add(jCancelarButton);
				jCancelarButton.setText("Cancelar");
				jCancelarButton.setBounds(272, 71, 79, 23);
				jCancelarButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jCancelarButtonActionPerformed(evt);
					}
				});
			}
			pack();
			this.setSize(400, 155);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void jEliminarButtonActionPerformed(ActionEvent evt) {
		String cerealSeleccionado = (String) jCerealComboBox.getSelectedItem();
		
		for (CerealView cerealView : cerealesView) {
			if(cerealView.getDescripcion().equals(cerealSeleccionado)) {
				try {
					if(cerealView.getEsFino()) {
						cerealController.borrarCerealFino(cerealView.getCodigo());
					} else {
						cerealController.borrarCerealGrueso(cerealView.getCodigo());
					}
				} catch (CerealInexistente e) {
					
				}
			}
		}
		this.setVisible(false);
		
	}
	
	private void jCancelarButtonActionPerformed(ActionEvent evt) {
		this.setVisible(false);
	}

}
